﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MarvinsArena.Core;
using System.Globalization;

namespace BattleEngineCommon
{
	public class HudStatusPanel
	{
		private Vector2 positionMissileFont;
		private Vector2 positionStatus;
		private Rectangle recHeat;
		private Rectangle recHitpoints;
		private Rectangle recPanel;

		public SpriteFont Font { get; set;}

		public Texture2D Texture { get; set; }

		public HudStatusPanel()
		{
            this.recHitpoints = new Rectangle(0, 0, 53, 109);
            this.recHeat = new Rectangle(56, 0, 54, 110);
            this.recPanel = new Rectangle(113, 0, 129, 129);

            this.positionStatus = new Vector2(0, 0);
		}

		public void Draw(SpriteBatch spriteBatch, IRobot robot, TournamentRules rules)
		{
            //Drawing panel
            spriteBatch.Draw(this.Texture, positionStatus, recPanel, Color.White);

            //Calculate  hitpoints height
            int hpheight = (int)(robot.Hitpoints * 1.0f / rules.Hitpoints * this.recHitpoints.Height);
            //Calculate hp position
            Vector2 positionHp;
            positionHp.X = (positionStatus.X + recPanel.Width ) / 2- recHitpoints.Width;
            positionHp.Y = (positionStatus.Y + recPanel.Height - recHitpoints.Height) / 2 ;
            //Drawing hp
            spriteBatch.Draw(
                this.Texture,
                positionHp,
                new Rectangle(this.recHitpoints.Height - hpheight, this.recHitpoints.Y, this.recHitpoints.Width, this.recHitpoints.Height),
                Color.Wheat);

            //Draw heats
            int heatHeight = (int)(robot.Heat * 1.0f / rules.MaxHeat * this.recHeat.Height);
            //Calculate heats position; 5px between hp and heat
            Vector2 heatPosition;
            heatPosition.X = (positionStatus.X + recPanel.Width) / 2;
            heatPosition.Y = (positionStatus.Y + recPanel.Height - recHeat.Height) / 2;
            spriteBatch.Draw(
                this.Texture,
                heatPosition,
                new Rectangle(this.recHeat.X, this.recHeat.Y, this.recHeat.Width, this.recHeat.Height - heatHeight),
                Color.Wheat);

            //Draw missile count
            string missileCount = robot.Missiles.ToString(CultureInfo.InvariantCulture);
            positionMissileFont = (positionStatus + new Vector2(recPanel.Width, recPanel.Height)) / 2;
            Vector2 fontOriginMissile = Font.MeasureString(missileCount) / 2;
            spriteBatch.DrawString(Font, missileCount, positionMissileFont, Color.White,
                0, fontOriginMissile, 1.0f, SpriteEffects.None, 0.5f);
		}
	}
}
